package org.dromara.admin.controller;

import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.dromara.admin.domain.bo.TemplateMarketBo;
import org.dromara.admin.domain.vo.TemplateMarketVo;
import org.dromara.admin.service.ITemplateMarketService;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.web.core.BaseController;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 后台端模板市场
 *
 * @author Lion Li
 * @date 2025-10-09
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/admin/templateMarket")
public class AdminTemplateMarketController extends BaseController {

    private final ITemplateMarketService templateMarketService;

    /**
     * 查询模板市场列表
     */
//    @SaCheckPermission("system:templateMarket:list")
    @GetMapping("/list")
    public TableDataInfo<TemplateMarketVo> list(TemplateMarketBo bo, PageQuery pageQuery) {
        return templateMarketService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出模板市场列表
     */
//    @SaCheckPermission("system:templateMarket:export")
    @Log(title = "模板市场", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(TemplateMarketBo bo, HttpServletResponse response) {
        List<TemplateMarketVo> list = templateMarketService.queryList(bo);
        ExcelUtil.exportExcel(list, "模板市场", TemplateMarketVo.class, response);
    }

    /**
     * 获取模板市场详细信息
     *
     * @param id 主键
     */
//    @SaCheckPermission("system:templateMarket:query")
    @GetMapping("/{id}")
    public R<TemplateMarketVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(templateMarketService.queryById(id));
    }

    /**
     * 新增模板市场
     */
//    @SaCheckPermission("system:templateMarket:add")
    @Log(title = "模板市场", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody TemplateMarketBo bo) {
        return toAjax(templateMarketService.insertByBo(bo));
    }

    /**
     * 修改模板市场
     */
//    @SaCheckPermission("system:templateMarket:edit")
    @Log(title = "模板市场", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody TemplateMarketBo bo) {
        return toAjax(templateMarketService.updateByBo(bo));
    }

    /**
     * 删除模板市场
     *
     * @param ids 主键串
     */
//    @SaCheckPermission("system:templateMarket:remove")
    @Log(title = "模板市场", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(templateMarketService.deleteWithValidByIds(List.of(ids), true));
    }
}
